package com.github.cakin.shiro.chapter17.web.controller;

import com.github.cakin.shiro.chapter17.entity.User;
import com.github.cakin.shiro.chapter17.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

/**
 * @className: UserController
 * @description: 用户管理控制器
 * @date: 2020/5/27
 * @author: cakin
 */
@Controller
@RequestMapping("/user")
public class UserController {
    /**
     * 用户服务
     */
    @Autowired
    private UserService userService;

    /**
     * 功能描述：展示用户列表
     *
     * @author cakin
     * @date 2020/5/26
     * @param model 展示给前端的数据
     * @return String 前端展示页面
     */
    @RequestMapping(method = RequestMethod.GET)
    public String list(Model model) {
        model.addAttribute("userList", userService.findAll());
        return "user/list";
    }

    /**
     * 功能描述：用户新增
     *
     * @author cakin
     * @date 2020/5/26
     * @param model 展示给前端的数据
     * @return String 前端展示页面
     */
    @RequestMapping(value = "/create", method = RequestMethod.GET)
    public String showCreateForm(Model model) {
        model.addAttribute("user", new User());
        model.addAttribute("op", "新增");
        return "user/edit";
    }
    /**
     * 功能描述：用户新增
     *
     * @author cakin
     * @date 2020/5/26
     * @param user 新增用户
     * @param redirectAttributes 重定向属性
     * @return String 重定向页
     */
    @RequestMapping(value = "/create", method = RequestMethod.POST)
    public String create(User user, RedirectAttributes redirectAttributes) {
        userService.createUser(user);
        redirectAttributes.addFlashAttribute("msg", "新增成功");
        return "redirect:/user";
    }

    /**
     * 功能描述：用户修改
     *
     * @author cakin
     * @date 2020/5/26
     * @param id 被修改用户id
     * @param model 展示给前端的数据
     * @return String 前端展示页面
     */
    @RequestMapping(value = "/{id}/update", method = RequestMethod.GET)
    public String showUpdateForm(@PathVariable("id") Long id, Model model) {
        model.addAttribute("user", userService.findOne(id));
        model.addAttribute("op", "修改");
        return "user/edit";
    }
    /**
     * 功能描述：用户修改
     *
     * @author cakin
     * @date 2020/5/26
     * @param user 被修改用户
     * @param redirectAttributes 重定向属性
     * @return String 重定向页面
     */
    @RequestMapping(value = "/{id}/update", method = RequestMethod.POST)
    public String update(User user, RedirectAttributes redirectAttributes) {
        userService.updateUser(user);
        redirectAttributes.addFlashAttribute("msg", "修改成功");
        return "redirect:/user";
    }

    /**
     * 功能描述：用户删除
     *
     * @author cakin
     * @date 2020/5/26
     * @param id 被删除用户id
     * @param model 展示给前端的数据
     * @return String 前端展示页面
     */
    @RequestMapping(value = "/{id}/delete", method = RequestMethod.GET)
    public String showDeleteForm(@PathVariable("id") Long id, Model model) {
        model.addAttribute("user", userService.findOne(id));
        model.addAttribute("op", "删除");
        return "user/edit";
    }
    /**
     * 功能描述：用户删除
     *
     * @author cakin
     * @date 2020/5/26
     * @param id 被删除用户id
     * @param redirectAttributes 重定向属性
     * @return String 重定向页面
     */
    @RequestMapping(value = "/{id}/delete", method = RequestMethod.POST)
    public String delete(@PathVariable("id") Long id, RedirectAttributes redirectAttributes) {
        userService.deleteUser(id);
        redirectAttributes.addFlashAttribute("msg", "删除成功");
        return "redirect:/user";
    }

    /**
     * 功能描述：修改用户密码
     *
     * @author cakin
     * @date 2020/5/26
     * @param id 修改用户密码的用户id
     * @param model 展示给前端的数据
     * @return String 前端展示页面
     */
    @RequestMapping(value = "/{id}/changePassword", method = RequestMethod.GET)
    public String showChangePasswordForm(@PathVariable("id") Long id, Model model) {
        model.addAttribute("user", userService.findOne(id));
        model.addAttribute("op", "修改密码");
        return "user/changePassword";
    }
    /**
     * 功能描述：修改用户密码
     *
     * @author cakin
     * @date 2020/5/26
     * @param id 修改用户密码的用户id
     * @param newPassword 新密码
     * @param redirectAttributes 重定向属性
     * @return String 重定向页面
     */
    @RequestMapping(value = "/{id}/changePassword", method = RequestMethod.POST)
    public String changePassword(@PathVariable("id") Long id, String newPassword, RedirectAttributes redirectAttributes) {
        userService.changePassword(id, newPassword);
        redirectAttributes.addFlashAttribute("msg", "修改密码成功");
        return "redirect:/user";
    }

}
